﻿using ImTools;
using Prism.Regions.Behaviors;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TangGuo.Resources;
using TangGuo.UserControls;
using static TangGuo.UserControls.AStarPath;

namespace TangGuo.Views
{
    /// <summary>
    /// 糖果争霸
    /// </summary>
    public partial class TangGuoZhengBa : UserControl
    {
        TangGuoMapBox[,] mapBoxs = new TangGuoMapBox[7, 7];
        List<TangGuoBox> tangGuoList; // 保存游戏场景中的所有糖果
        PositionGuangHuan guangHuan; // 光环

        readonly int[][] GameStartTangGuolist = new int[10][] {
            new int[3] {4, 0, 1}, new int[3] {0, 1, 2 }, new int[3] {2, 2, 3 }, new int[3] {1, 3, 2}, new int[3] { 3, 3, 2 },
            new int[3] {0, 4, 1}, new int[3] {1, 4, 1 }, new int[3] {3, 4, 2 }, new int[3] {5, 4, 1}, new int[3] { 2, 5, 1 }}; // 开局糖果

        public static int maxNumber; // 合成的最大的糖果级别
        public static int shovleNumber; // 可使用的铲子的数量
        public static int score; // 当前得分

        int[] preAddTangguo = new int[4]; // 保存下一步要加添加到地图中的数字

        bool IsPathMoveAnimationCompleted = false;  // 沿路径移动动画结束标志
        bool IsLineMoveAnimationCompleted = false; // 沿直线移动动画结束标志
        bool IsUpgradeAnimationCompleted = false; // 糖果升级动画结束标志
        bool IsAddNewAnimationCompleted = false; // 添加新糖果动画结束标志

        public TangGuoZhengBa()
        {
            InitializeComponent();
            guangHuan = new();
            guangHuan.SetPosition(4, 4);
            GameBoard.Children.Add(guangHuan);
            tangGuoList = new();
            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    mapBoxs[i, j] = new();
                    mapBoxs[i, j].SetPos(i, j);
                    GameBoard.Children.Add(mapBoxs[i, j]);
                }
            }
            ReStartGame();
        }

        /// <summary>
        /// 生启游戏
        /// </summary>
        public void ReStartGame()
        {
            if (tangGuoList != null)
            {
                foreach (TangGuoBox item in tangGuoList)
                {
                    //item.SetDad();
                    GameBoard.Children.Remove(item);
                }
                tangGuoList.Clear();
            }
            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    mapBoxs[i, j].SetVisible();
                }
            }
            // 以下设置初始数字，及位置，可根据需要修改
            for (int i = 0; i < GameStartTangGuolist.Length; i++)
            {
                int col = GameStartTangGuolist[i][0];
                int row = GameStartTangGuolist[i][1];
                int number = GameStartTangGuolist[i][2];
                maxNumber = Math.Max(maxNumber, number);
                TangGuoBox box = new TangGuoBox(col, row, number);
                box.SetLocation();
                tangGuoList.Add(box);
                GameBoard.Children.Add((TangGuoBox)box);
                mapBoxs[col, row].SetHidden();
            }
            for (int i = 0; i < preAddTangguo.Length; i++) { preAddTangguo[i] = i; }
            SetPreAddTangguo();
            guangHuan.LightGuangHuan.Visibility = Visibility.Hidden;
            PublicData.LastSelectIsNumber = false;

            shovleNumber = 5;
            TangGuoSettings.Default.ShovelNumber=shovleNumber;
            TangGuoSettings.Default.Save();

            score = 0;
            TotalScore.Text = "0";

            MaskLayer.Visibility = Visibility.Hidden;

        }

        /// <summary>
        /// 生产待添加糖果，显示在游戏区下方
        /// </summary>
        private void SetPreAddTangguo()
        {

            Random random = new Random();
            int minNumber = Math.Max(maxNumber - 5, 1);
            for (int i = 0; i < preAddTangguo.Length; i++)
            {
                preAddTangguo[i] = random.Next(minNumber, maxNumber);

            }
            preAdd0.Fill = this.FindResource($"TangGuo_{preAddTangguo[0]}") as ImageBrush;
            preAdd1.Fill = this.FindResource($"TangGuo_{preAddTangguo[1]}") as ImageBrush;
            preAdd2.Fill = this.FindResource($"TangGuo_{preAddTangguo[2]}") as ImageBrush;
            preAdd3.Fill = this.FindResource($"TangGuo_{preAddTangguo[3]}") as ImageBrush;

        }

        /// <summary>
        /// 给待添加的糖果，设置位置信息
        /// </summary>
        /// <returns></returns>
        private List<TangGuoBox> AddTangguoPos()
        {
            List<BasePos> blankList = new List<BasePos>(); // 空闲位置列表
            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    bool isTangGuo = false;
                    foreach (TangGuoBox item in tangGuoList)
                    {
                        if (item != null && item.Col == i && item.Row == j) isTangGuo = true;
                    }
                    if (isTangGuo == false && !(i == 6 && j % 2 == 1)) blankList.Add(new(i, j)); // 将所有空闲位置添加到列表中
                }
            }

            Random random = new Random();
            List<TangGuoBox> newTangGuoList = new();
            List<int> indexlist = new List<int>();
            for (int i = 0; i < blankList.Count && i < preAddTangguo.Length; i++)
            {
                int index = random.Next(blankList.Count); // 随机生成一个索引值
                while (indexlist.Contains(index))
                {
                    index = random.Next(blankList.Count); // 索引值如果重复，则重新生成，直到找到不重复的索引值
                }
                indexlist.Add(index);
                newTangGuoList.Add(new TangGuoBox(blankList[index].Col, blankList[index].Row, preAddTangguo[i]));
            }
            return newTangGuoList;
        }

        TangGuoBox lastSelectedTangGuo = null; // 上次点击时选中的糖果

        /// <summary>
        /// 当鼠标松开时，处理相关操作。本函数是游戏操控的主函数。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            List<Storyboard> storyboardLists = new();
            ClenaTangGuoList();  // 清理糖果列表，将设置为DAD状态的糖果从列表和游戏场景中删除
            if (e.Source.GetType().Equals(typeof(TangGuoBox)))  //如果选中的是糖果，则给该糖果加一光环
            {
                lastSelectedTangGuo = (TangGuoBox)e.Source;
                guangHuan.SetPosition(lastSelectedTangGuo.Col, lastSelectedTangGuo.Row);
                guangHuan.LightGuangHuan.Opacity = 0.1;
                guangHuan.LightGuangHuan.Visibility = Visibility.Visible;
            }
            if (e.Source.GetType().Equals(typeof(TangGuoMapBox)))  // 如果选中的是空位，则把已选中的糖果移动到此空位
            {
                MaskLayer.Visibility = Visibility.Visible; // 加一个遮罩，在处理动画期间，不再接受鼠标点击事件
                TangGuoMapBox selectedMapBox = (TangGuoMapBox)e.Source;
                guangHuan.SetPosition(selectedMapBox.Col, selectedMapBox.Row);
                guangHuan.LightGuangHuan.Visibility = Visibility.Hidden;
                if (lastSelectedTangGuo != null) // 如果前一次点击的不是空位
                {
                    BasePos fromPos = lastSelectedTangGuo.GetBasePos();
                    BasePos toPos = selectedMapBox.GetBasePos();
                    List<BasePos> pathPosList = FindPath(fromPos, toPos);  // 计算移动路径
                    if (pathPosList != null && pathPosList.Count > 0)  // 路径列表不为空时
                    {
                        MoveByPath(lastSelectedTangGuo, pathPosList); // 沿路径移动糖果
                        List<TangGuoBox> sameTangList = FindSameTangGuo(tangGuoList, lastSelectedTangGuo);  // 查找相邻糖果
                        if (sameTangList != null && sameTangList.Count >= 3)  // 相邻糖果达到3个
                        {
                            MergeTangGuo(lastSelectedTangGuo);// 合并、升级糖果
                        }
                        else // 移动完后，如没有发生合并升级，则添加新糖果
                        {
                            List<TangGuoBox> tangList = AddTangguoPos();
                            if (tangList == null) return;
                            AddNewToGame(tangList);
                            for (int i = 0; i < tangList.Count; i++)
                            {
                                MergeTangGuo(tangList[i]); // 添加糖果后，逐个检查是否可以合并，如是，则合并
                            }
                            SetPreAddTangguo(); // 重新生成下一步要添加的数字
                        }
                    }
                    else // 路径列表为空，则表示没有找到移动路径
                    {
                        // 此处添加提示代码，显示未找到路径
                    }
                }
                lastSelectedTangGuo = null; // 所有动作处理完毕后，将预选糖果设为空
                PublicData.LastSelectIsNumber = false;
                SaveGameData();
                MaskLayer.Visibility = Visibility.Hidden;
            }
        }

        /// <summary>
        /// 保存游戏数据
        /// </summary>
        private void SaveGameData()
        {
            TangGuoSettings.Default.MaxTangGuo = maxNumber;
            TangGuoSettings.Default.Save();
        }


        int waitTimePie = 10; // 等待动画结束时，循环检查的时间间隔（ms）

        /// <summary>
        /// 添加新糖果
        /// </summary>
        /// <param name="tangList"></param>
        public void AddNewToGame(List<TangGuoBox> tangList)
        {
            List<UpgradeEffect> UpgradeEffectList = new List<UpgradeEffect>();

            for (int i = 0; i < tangList.Count; i++)
            {
                TangGuoBox tangguo = tangList[i];
                GameBoard.Children.Add(tangguo);  // 添加新糖果
                tangGuoList.Add(tangguo);
                tangguo.SetLocation();
                mapBoxs[tangguo.Col, tangguo.Row].SetHidden();

                UpgradeEffect upgrade = new(tangList[i].Col, tangList[i].Row);
                GameBoard.Children.Add(upgrade);
                UpgradeEffectList.Add(upgrade);
            }

            DoubleAnimation effect0 = new()
            {
                From = 0.0,
                To = 0.3,
                Duration = TimeSpan.FromMilliseconds(200),
            };
            effect0.Completed += AddNew_StoryBoard_completed;
            effect0.AutoReverse = true;


            foreach (UpgradeEffect effect in UpgradeEffectList)
            {
                effect.BeginAnimation(UpgradeEffect.OpacityProperty, effect0);
            }
            IsAddNewAnimationCompleted = false;
            while (IsAddNewAnimationCompleted == false)
            {
                PublicFunction.Delay(waitTimePie);
            }
        }

        /// <summary>
        /// 添加新糖果动画结束后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddNew_StoryBoard_completed(object sender, EventArgs e)
        {
            foreach (TangGuoBox tang in tangGuoList) { tang.Storyboard_Completed(sender, e); }  // 将所有糖果设置为合并后状态
            ClenaTangGuoList(); // 清理游戏场景
            List<UpgradeEffect> effectList = new();
            foreach (var pie in GameBoard.Children)
            {
                if (pie.GetType() == typeof(UpgradeEffect))
                {
                    effectList.Add((UpgradeEffect)pie);
                }
            }
            foreach (UpgradeEffect effect in effectList)
            {
                effect.Visibility = Visibility.Hidden;
                GameBoard.Children.Remove(effect);
            }
            IsAddNewAnimationCompleted = true;
        }

        /// <summary>
        /// 沿路径移动糖果
        /// </summary>
        /// <param name="tangGuo">需移动的糖果</param>
        /// <param name="posList">路径点列表</param>
        public void MoveByPath(TangGuoBox tangGuo, List<BasePos> posList)
        {
            if (posList.Count == 0) return;
            tangGuo.SetPos(posList[posList.Count - 1]);
            string pathstring = $"M {TangGuoBox.GetXByPos(posList[0])} , {TangGuoBox.GetYByPos(posList[0])}";  // 路径转换为PATH格式数据
            foreach (BasePos point in posList)
            {
                pathstring += $"L {TangGuoBox.GetXByPos(point)} , {TangGuoBox.GetYByPos(point)} ";
            }
            Path path1 = new Path();
            path1.Data = Geometry.Parse(pathstring); // 生成路径数据

            DoubleAnimationUsingPath pathX = new()
            {
                Duration = TimeSpan.FromMilliseconds(posList.Count * 50),
                PathGeometry = path1.Data.GetFlattenedPathGeometry(),
                Source = PathAnimationSource.X
            };
            Storyboard.SetTargetProperty(pathX, new PropertyPath(Canvas.LeftProperty));
            Storyboard.SetTargetName(pathX, tangGuo.Name);
            DoubleAnimationUsingPath pathY = new()
            {
                Duration = TimeSpan.FromMilliseconds(posList.Count * 50),
                PathGeometry = path1.Data.GetFlattenedPathGeometry(),
                Source = PathAnimationSource.Y
            };
            Storyboard.SetTargetProperty(pathY, new PropertyPath(Canvas.TopProperty));
            Storyboard.SetTargetName(pathY, tangGuo.Name);
            Storyboard storyboard = new() { FillBehavior = FillBehavior.Stop, AutoReverse = false };
            storyboard.Completed += MoveByPath_Storyboard_Completed;
            storyboard.Children.Add(pathX);
            storyboard.Children.Add(pathY);
            storyboard.Begin(tangGuo);
            mapBoxs[posList[0].Col, posList[0].Row].SetVisible();
            IsPathMoveAnimationCompleted = false;
            while (IsPathMoveAnimationCompleted == false)
            {
                PublicFunction.Delay(waitTimePie);
            }
            mapBoxs[tangGuo.Col, tangGuo.Row].SetHidden();


        }
        /// <summary>
        /// 糖果沿路径移动完毕后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MoveByPath_Storyboard_Completed(object sender, EventArgs e)
        {
            lastSelectedTangGuo.Storyboard_Completed(sender, e);
            IsPathMoveAnimationCompleted = true;
        }

        /// <summary>
        /// 合并糖果
        /// </summary>
        /// <param name="merge"></param>
        private void MergeTangGuo(TangGuoBox merge)
        {
            List<TangGuoBox> sameTangList = FindSameTangGuo(tangGuoList, merge); // 查找相邻糖果
            if (sameTangList == null || sameTangList.Count < 3) return;

            int num = merge.GetNumber();
            maxNumber = Math.Max(maxNumber, num+1);
            int baseScoreStandard = 10 + (sameTangList.Count - 3) * 5; // 合并时，得分基数为10分，超过3个时，每个再加5分
            score+= sameTangList.Count * num * baseScoreStandard; // 得分 = 合并个数 * 糖果级别 * 得分基数
            
            foreach (TangGuoBox tangguo in sameTangList)
            {
                MoveToPosAndSetDad(tangguo, merge.GetBasePos());  // 相邻的糖果同时向目标糖果移动
            }
            IsLineMoveAnimationCompleted = false;
            while (IsLineMoveAnimationCompleted == false)
            {
                PublicFunction.Delay(waitTimePie);
            }
            RefreshTotalScoreText(score);

            UpgradeTangGuo(merge, num+1);
            MergeTangGuo(merge);
        }

        /// <summary>
        /// 合并糖果时，计算得分
        /// </summary>
        /// <param name="score"></param>
        private void RefreshTotalScoreText(int score)
        {
            int num0 = score % 10000;
            int num1 = score / 10000;
            string str = (num1 > 0) ? num1.ToString() + "," : "";
            TotalScore.Text = str+num0.ToString();
        }

        /// <summary>
        /// 相邻的糖果移动到合并位置后，清除
        /// </summary>
        /// <param name="tangGuo"></param>
        /// <param name="toPos"></param>
        public void MoveToPosAndSetDad(TangGuoBox tangGuo, BasePos toPos)
        {
            if (tangGuo == null || toPos == null) return;
            mapBoxs[tangGuo.Col, tangGuo.Row].SetVisible();
            tangGuo.SetPos(toPos);
            tangGuo.SetDadOnMoveCompleted();
            DoubleAnimation moveX = new() { To = TangGuoBox.GetXByPos(toPos), Duration = TimeSpan.FromMilliseconds(100) };
            Storyboard.SetTargetProperty(moveX, new PropertyPath(Canvas.LeftProperty));
            Storyboard.SetTargetName(moveX, tangGuo.Name);

            DoubleAnimation moveY = new() { To = TangGuoBox.GetYByPos(toPos), Duration = TimeSpan.FromMilliseconds(100) };
            Storyboard.SetTargetProperty(moveY, new PropertyPath(Canvas.TopProperty));
            Storyboard.SetTargetName(moveY, tangGuo.Name);

            DoubleAnimation makeDad = new() { From = 1.0, To = 0.0, BeginTime = TimeSpan.FromMilliseconds(100), Duration = TimeSpan.FromMilliseconds(10) };
            Storyboard.SetTargetProperty(makeDad, new PropertyPath(OpacityProperty));
            Storyboard.SetTargetName(makeDad, tangGuo.Name);

            Storyboard storyboard = new() { FillBehavior = FillBehavior.HoldEnd, AutoReverse = false };
            storyboard.Completed += MoveToPosAndSetDad_Storyboard_Completed;
            storyboard.Children.Add(moveX);
            storyboard.Children.Add(moveY);
            storyboard.Children.Add(makeDad);
            storyboard.Begin(tangGuo);

        }

        /// <summary>
        /// 相邻糖果移动动画结束后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MoveToPosAndSetDad_Storyboard_Completed(object sender, EventArgs e)
        {
            foreach (TangGuoBox tang in tangGuoList) { tang.Storyboard_Completed(sender, e); }  // 将所有糖果设置为合并后状态
            ClenaTangGuoList(); // 清理游戏场景
            IsLineMoveAnimationCompleted = true;
        }

        /// <summary>
        /// 合并后，升级糖果
        /// </summary>
        /// <param name="tangGuo">需升级的糖果</param>
        /// <param name="num">级别数值</param>
        public void UpgradeTangGuo(TangGuoBox tangGuo, int num)
        {
            // 此处添加糖果升级动画
            tangGuo.SetNumber(num);

            UpgradeEffect upgrade = new(tangGuo.Col, tangGuo.Row);
            GameBoard.Children.Add(upgrade);
            Storyboard storyboard = this.FindResource("TangGuoUpgradeEffectAnimation") as Storyboard; // 升级时的动画效果
            storyboard.Completed += UpgradeEffect_StoryBoard_completed;
            storyboard.Begin(upgrade.rect);
            
            IsUpgradeAnimationCompleted = false;
            while (IsUpgradeAnimationCompleted == false)
            {
                PublicFunction.Delay(waitTimePie);
            }
        }

        /// <summary>
        /// 糖果升级动画结束后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpgradeEffect_StoryBoard_completed(object sender, EventArgs e)
        {
            foreach (TangGuoBox tang in tangGuoList) { tang.Storyboard_Completed(sender, e); }  // 将所有糖果设置为合并后状态
            List<UpgradeEffect> effectList = new();
            foreach (var pie in GameBoard.Children)
            {
                if (pie.GetType() == typeof(UpgradeEffect))
                {
                    effectList.Add((UpgradeEffect)pie);
                }
            }
            foreach (UpgradeEffect effect in effectList)
            {
                effect.Visibility = Visibility.Hidden;
                GameBoard.Children.Remove(effect);  // 从游戏场景中删除
            }
            IsUpgradeAnimationCompleted = true;

        }

        /// <summary>
        /// 查找移动路径
        /// </summary>
        /// <param name="fromPos">出发位置</param>
        /// <param name="toPos">目标位置</param>
        /// <returns>路径列表</returns>
        public List<BasePos> FindPath(BasePos fromPos, BasePos toPos)
        {
            if (fromPos == null || toPos == null) return null;
            int[,] numMap = TangGuoListConverToIntArray(tangGuoList);
            return AStarPath.FindPath(numMap, fromPos, toPos);
        }

        /// <summary>
        /// 查找相邻的、大小相同的糖果
        /// </summary>
        /// <param name="tangguo">指定糖果</param>
        /// <returns>相邻糖果列表</returns>
        public static List<TangGuoBox> FindSameTangGuo(List<TangGuoBox> tangList, TangGuoBox tangguo)
        {
            if (tangguo == null) return null;
            int[,] numMap = TangGuoListConverToIntArray(tangList);
            List<BasePos> poslist = AStarPath.FindSameTile(numMap, new BasePos(tangguo.Col, tangguo.Row));
            if (poslist == null) return null;
            List<TangGuoBox> tanglist = new List<TangGuoBox>();
            foreach (BasePos pos in poslist)
            {
                foreach (TangGuoBox tangGuoBox in tangList)
                {
                    if (pos.IsSamePos(tangGuoBox.GetBasePos())) tanglist.Add(tangGuoBox);
                }
            }
            return tanglist;
        }

        /// <summary>
        /// 糖果数据转换为二维数组，用于传递的路径算法
        /// </summary>
        /// <param name="tangList"></param>
        /// <returns></returns>
        public static int[,] TangGuoListConverToIntArray(List<TangGuoBox> tangList)
        {
            if (tangList == null) return null;
            int[,] numMap = new int[7, 7];
            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    numMap[i, j] = 0;
                }
            }
            foreach (TangGuoBox item in tangList)
            {
                if (item != null) numMap[item.Col, item.Row] = item.GetNumber();
            }
            return numMap;
        }


        /// <summary>
        /// 清理糖果列表，将设置为DAD状态的糖果从列表和游戏场景中删除
        /// </summary>
        private void ClenaTangGuoList()
        {
            List<TangGuoBox> dadList = new();
            foreach (TangGuoBox tang in tangGuoList)
            {
                if (tang.IsDad()) dadList.Add(tang);
            }
            if (dadList == null) return;
            foreach (TangGuoBox dadtang in dadList)
            {
                tangGuoList.Remove(dadtang);
                GameBoard.Children.Remove(dadtang);
            }

        }

        /// <summary>
        /// 铲子按下时，阴影缩小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Showle_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ShovleShadowEffect.ShadowDepth = 10;
        }

        /// <summary>
        /// 点击铲子时，消除3个最低等级的糖果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Shovle_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ShovleShadowEffect.ShadowDepth = 30;
            if (shovleNumber>0) { DeleteThreeMinTangGuo(); }
            shovleNumber--;
            if (shovleNumber < 0) { shovleNumber = 0; }
            ShovleNumber.Text = string.Format("{0:G}/10", shovleNumber);
            TangGuoSettings.Default.ShovelNumber = shovleNumber;
            TangGuoSettings.Default.Save();
        }

        /// <summary>
        /// 消除3个最低等级的糖果
        /// </summary>
        private void DeleteThreeMinTangGuo()
        {
            if (tangGuoList == null) return;
            if (tangGuoList.Count < 3) return;
            int minNumber = maxNumber;
            tangGuoList=tangGuoList.OrderBy(x=>x.GetNumber()).ToList();  // 按糖果等级升序排列

            TangGuoBox tang0 = tangGuoList.ElementAt(0);
            TangGuoBox tang1 = tangGuoList.ElementAt(1);
            TangGuoBox tang2 = tangGuoList.ElementAt(2);
            mapBoxs[tang0.Col, tang0.Row].SetVisible();
            mapBoxs[tang1.Col, tang1.Row].SetVisible();
            mapBoxs[tang2.Col, tang2.Row].SetVisible();
            if (GameBoard.Children.Contains(tang0)) GameBoard.Children.Remove(tang0);
            tangGuoList.Remove(tang0);
            if (GameBoard.Children.Contains(tang1)) GameBoard.Children.Remove(tang1);
            tangGuoList.Remove(tang1);
            if (GameBoard.Children.Contains(tang2)) GameBoard.Children.Remove(tang2);
            tangGuoList.Remove(tang2);

        }

        /// <summary>
        /// 重新开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RestartButtonClick(object sender, RoutedEventArgs e)
        {
            ReStartGame();
        }

        /// <summary>
        /// 遮罩存在时，鼠标点击事件置为“已处理”，事件不再向上冒泡
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMaskLayerClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
    }
}
